<?php
/**
 * Copyright (c) 不争网络
 *
 * @Description     个人中心.
 * @Author          Evelyn丶好久不见
 * @Copyright       Copyright (c) 不争网络 保留所有版权()
 * @Date:           2019/4/28
 */
namespace app\api\controller;
use app\common\controller\CommonApi;
use think\Db;
class User extends CommonApi{
    public function getuserinfo(){
        $info = model('user')->getUserInfo(['id'=>UID]);
        $this->apiReturn(1,'用户信息',$info);
    }

//收藏列表
    public function collectionlist(){
        $list = db('collection_goods')->alias('c')->field('goods_id,title,banner as image,price')->join('goods g','g.id = c.goods_id','left')->where('user_id',UID)->order('c.add_time desc')->limit(($this->p-1)*10,10)->select();
        foreach ($list as $key => $value) {
           $list[$key]['image'] = $this->host.explode(',',$value['image'])[0];
        }
        $this->apiReturn(1,'收藏列表',$list);
    }

//收藏商品
    public function collection(){
        $type = input('type',1);
        $id = input('id',0);
        $info = db('goods')->where(['id'=>$id])->find();
        if(!$info)
            $this->apireturn(0,'商品信息错误');
        if($type == 1){
            $query = db('collection_goods')->insert(['user_id'=>UID,'goods_id'=>$id,'add_time'=>time()]);
        }else{
            $query = db('collection_goods')->where(['user_id'=>UID,'goods_id'=>$id])->delete();
        } 
        if(!$query)
            $this->apireturn(0,'操作失败');
        $this->apireturn(1,'操作成功');
    }

//意见反馈
    public function opinion(){
    	$upload_images='';
    	if(isset($_FILES['images']) && $_FILES['images']!=''){
        	$upload_images = implode(',',model('user')->upload('images','opinion')['data']);
        }
        //echo"<pre>";print_r($upload_images);die;
        $data['image'] = $upload_images;
        $data['content'] = input('content');
        $data['add_time'] = time();
        $data['user_id'] = UID;

        $query = db('opinion')->insert($data);
        if($query)
        	$this->apiReturn(1,'提交成功');
        $this->apiReturn(0,'提交失败');
    }

//修改用户资料
    public function userinfo(){
    
        $data = [];
        if(isset($_FILES['photo_path']) && $_FILES['photo_path']!=''){
           // print_r(implode(',',model('user')->upload('photo_path','photopath')['data']));die;
            $data['photo_path'] = $this->host.implode(',',model('user')->upload('photo_path','photopath')['data']);
        }
       // print_r($data);die;
        $nickname = input('nickname','');
        if($nickname!=''){
            $data['nickname'] = $nickname;
        }

        $birthday = input('birthday','');
        if($birthday!=''){
            $data['birthday'] = $birthday;
        }

        $sex= input('sex',0);
        if($sex!=0){
            $data['sex'] = $sex;
        }

        $account = input('account','');
        if($account!=''){
            $check = model('send')->checkCode($account,input('code',0),3);
            if($check['status']==0){
                $this->apiReturn(0,$check['msg']);
            }
            $data['account'] = $account;
        }

        $query = db('user')->where('id',UID)->update($data);
        if(!$query)
            $this->apiReturn(0,'操作失败');
        $this->apiReturn(1,'操作成功',model('user')->getUserInfo(['id'=>UID]));
    }

    //账号注销
    public function out(){
        $query = db('user')->where('id',UID)->delete();
        if($query)
            $this->apiReturn(1,'注销成功');
        $thsi->apiReturn(0,'注销失败');
        $this->apiReturn();
    }
}